package com.rightsidetech.http.controller.storeApp.address;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.commom.utils.HttpClient;
import com.rightsidetech.commom.utils.StringUtil;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.store.StoreAddressSaveReq;
import com.rightsidetech.system.dtos.request.store.StoreAddressUpReq;
import com.rightsidetech.system.dtos.response.store.StoreAddressResp;
import com.rightsidetech.system.service.SysStoreAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/storeApp/address")
@ShenyuSpringMvcClient(path = "/storeApp/address/**")
@Api(tags = "门店地址相关")
public class SysStoreAddressController extends BaseController {

    @Resource
    private SysStoreAddressService sysStoreAddressService;

    @Value("${restapi.amap.addressUrl}")
    private String addressUrl;
    @Value("${restapi.amap.key}")
    private String amapKey;

    @Log
    @Token
    @PostMapping("/save")
    @ApiOperation("添加地址")
    public ResponseData saveAddress(@RequestBody StoreAddressSaveReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Map map = new HashMap<>();
        map.put("key", amapKey);
        map.put("address", req.getAddress());
        JSONObject jsonObject = JSON.parseObject(HttpClient.sendGet(addressUrl, map));
        if (!StringUtil.isNullorEmpty(jsonObject) && jsonObject.getString("status").equals("1") && !StringUtil.isNullorEmpty(jsonObject.get("geocodes"))) {
            JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
            JSONObject jsonObject2 = jsonArray.getJSONObject(0);

            if (!StringUtil.isNullorEmpty(jsonObject2.getString("location"))) {
                req.setProvice(jsonObject2.getString("province"));
                req.setCity(jsonObject2.getString("city"));
                req.setArea(jsonObject2.getString("district"));
            } else {
                return ResponseData.fail("地址错误, 请重新输入地址");
            }
        } else {
            return ResponseData.fail("地址错误, 请重新输入地址");
        }
        Long storeId = Long.parseLong(getAttribute());
        return sysStoreAddressService.saveAddress(req,storeId);
    }

    @Log
    @Token
    @GetMapping("/list")
    @ApiOperation("门店地址列表")
    public ResponseData<List<StoreAddressResp>> addressList(){
        Long storeId = Long.parseLong(getAttribute());
        return sysStoreAddressService.addressList(storeId);
    }

    @Log
    @Token
    @PostMapping("/update")
    @ApiOperation("编辑地址")
    public ResponseData updateAddress(@RequestBody StoreAddressUpReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Map map = new HashMap<>();
        map.put("key", amapKey);
        map.put("address", req.getAddress());
        JSONObject jsonObject = JSON.parseObject(HttpClient.sendGet(addressUrl, map));
        if (!StringUtil.isNullorEmpty(jsonObject) && jsonObject.getString("status").equals("1") && !StringUtil.isNullorEmpty(jsonObject.get("geocodes"))) {
            JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
            JSONObject jsonObject2 = jsonArray.getJSONObject(0);

            if (!StringUtil.isNullorEmpty(jsonObject2.getString("location"))) {
                req.setProvice(jsonObject2.getString("province"));
                req.setCity(jsonObject2.getString("city"));
                req.setArea(jsonObject2.getString("district"));
            } else {
                return ResponseData.fail("地址错误, 请重新输入地址");
            }
        } else {
            return ResponseData.fail("地址错误, 请重新输入地址");
        }
        Long storeId = Long.parseLong(getAttribute());
        return sysStoreAddressService.update(req, storeId);
    }

    @Log
    @Token
    @PostMapping("/del")
    @ApiOperation("删除地址")
    public ResponseData delAddress(@RequestBody DelReq req){
        return sysStoreAddressService.del(req, Long.parseLong(getAttribute()));
    }

}
